package com.cjbdi.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.cjbdi.dao.GonggaoRepository;
import com.cjbdi.dao.UserGonggaoRepository;
import com.cjbdi.dao.UserRepository;
import com.cjbdi.entity.GongGao;
import com.cjbdi.entity.UserInfo;
import com.cjbdi.pagination.PageInfo;
import com.cjbdi.util.CopyUtil;

@Service
@Transactional
public class GonggaoService {
	@Autowired
    private GonggaoRepository gonggaoRepository;
	
    public PageInfo<GongGao> page(Pageable pageable) {
        Page<GongGao> page = gonggaoRepository.findAll(pageable);
        int records = (int) page.getTotalElements();
        int pageSize = page.getSize();
        int total = records % pageSize == 0 ? records / pageSize : records / pageSize + 1;
        PageInfo<GongGao> pageInfo = new PageInfo<>();
        pageInfo.setPage(page.getNumber() + 1);//页码
        pageInfo.setPageSize(pageSize);//页面大小
        pageInfo.setRows(page.getContent());//分页结果
        pageInfo.setRecords(records);//总记录数
        pageInfo.setTotal(total);//总页数
        return pageInfo;
    }
    
    public void save(GongGao gaonggao) {
    	gonggaoRepository.save(gaonggao);

    }
    public List<GongGao> findByTitle(String title){
    	List<GongGao> list = gonggaoRepository.findByTitle(title);
    	return list;
    }
    
    public List<GongGao> findById(int id){
    	List<GongGao> list = gonggaoRepository.findById(id);
    	return list;
    }
    
	public boolean delete(int id) {
		// TODO Auto-generated method stub
		Integer i = gonggaoRepository.delete(id);
    	if(i>0) {
    		return true;
    	}else {
    		return false;
    	}
	}
	public PageInfo<GongGao> getPageByParam(String title,String fymc,String tag,Pageable pageable){
		Page<GongGao> page = gonggaoRepository.getPageByParam(title, fymc, tag, pageable);
		int records = (int) page.getTotalElements();
        int pageSize = page.getSize();
        int total = records % pageSize == 0 ? records / pageSize : records / pageSize + 1;
        PageInfo<GongGao> pageInfo = new PageInfo<>();
        pageInfo.setPage(page.getNumber() + 1);//页码
        pageInfo.setPageSize(pageSize);//页面大小
        pageInfo.setRows(page.getContent());//分页结果
        pageInfo.setRecords(records);//总记录数
        pageInfo.setTotal(total);//总页数
		return pageInfo;
	}
	
	public List<GongGao> getLastGonggao(String fybm){
		List<GongGao> list = gonggaoRepository.getLastGonggao(fybm);
    	return list;
	}
	
}
